﻿package com.findMoneyGame.pages {
	
	import flash.display.MovieClip;
	import flash.display.DisplayObject;
	import flash.events.Event;

	import com.findMoneyGame.Engine;
	import com.findMoneyGame.models.User;
	import com.findMoneyGame.pages.WrappingPage;
	import com.findMoneyGame.pages.buyMoney.BuyMoneyPage;
	import com.findMoneyGame.pages.game.GameLevelPage;
	import com.findMoneyGame.pages.game.states.LoadingState;
	
	
	public class GamePage extends WrappingPage {
		private function get user () { return User.getInst(); }
		
		public function GamePage(engine : Engine) {
			super(engine);
			gameLevel.addEventListener(ClosablePage.CLOSE_EVENT, closeGameLevel);
			gameLevel.addEventListener(GameLevelPage.LEVEL_COMPLETE, levelComplete);
			gameLevel.addEventListener(GameLevelPage.NEED_MORE_MONEY, showBuyMoneyPage);
			new LoadingState(gameLevel).start();
		}

		protected override function changeState (newState : DisplayObject) {
			disableGameLevel();
			super.changeState(newState);
		}
		protected override function activatePage () {
			gameLevel.startGame();
			super.activatePage();
		}
		protected override function deactivatePage () {
			gameLevel.stopGame();
			super.deactivatePage();
		}

		private function closeGameLevel (e : Event) { changeState(new HomePage(engine)); }

		private function levelComplete (e : Event) { changeState(new GamePage(engine)); }

		private function showBuyMoneyPage (e : Event) { showDialog(new BuyMoneyPage()); }
		
		private function disableGameLevel () { gameLevel.stopGame(); }
	}
	
}
